<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '起重机类型分布',
          textStyle: {
            color: 'yellow'
          },
          x: '5%',
          y: '5%'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: [
          {
            x: '65%',
            y: '30%',
            data: ['桥式', '塔式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '65%',
            y: '40%',
            data: ['门式', '门座式'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '65%',
            y: '50%',
            data: ['流动式', '架桥机'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          },
          {
            x: '65%',
            y: '60%',
            data: ['其他类型'],
            inactiveColor: '#5e5e5e',
            textStyle: {
              color: '#ffffff'
            }
          }
        ],
        series: [
          {
            name: '起重机类型分布',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 85],
            center: ['40%', '60%'],
            data: [
              { value: 1054, name: '桥式' },
              { value: 832, name: '塔式' },
              { value: 567, name: '门式' },
              { value: 342, name: '门座式' },
              { value: 231, name: '流动式' },
              { value: 123, name: '架桥机' },
              { value: 223, name: '其他类型' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
